package Entity;

import java.util.ArrayList;
import java.util.List;

import Controllers.GenericToolsForController.RequestMainStatusEnum;
import Controllers.GenericToolsForController.RequestStepStatusEnum;



//  This class is meant to be the way we arrange requests. it will be basically an array of request with few extra abilities.

public class RequestArray extends ArrayList<Request>{
//	private String[] generalCommonFields = {"Request ID", "Initiator Name", "info system", "status","date"};
//	private String[] evaluationCommonFields = {"Request ID", "Initiator Name", "inspector name","info system","Notes"};

	/**
	 * 
	 */
	private static final long serialVersionUID = -3214182107674882873L;

	/**
	 * evluation fileds and titles
	 * @return
	 */
	public static String[] getEvaluationCommonFields() 
	{
		return new String[]{"Request ID", "Initiator Name", "inspector name","info system","Notes"};
	}
/**
 * testing titles
 * @return
 */
	public static String[] getTestingCommonFields() 
	{
		return new String[]{"Request ID", "Initiator Name", "inspector name","info system","Notes"};
	}
/**
 * genreal titles
 * @return
 */
	public static String[] getGeneralCommonFields()
	{
		return new String[]{"Request ID", "Initiator Name", "info system", "status","date"};
	}
	/**
	 * constructor
	 */
	public RequestArray() 
	{
		super();
	}	
	/**
	 * constructor
	 * @param listOfRequests
	 */
	public RequestArray(List<Request> listOfRequests)
	{
          for (Request req: listOfRequests)
          {
        	  add(req);
          }
	}
	/**
	 *  matrix of strings with data
	 * @return String[][]
	 */
	public String[][] requestArrayToMatrixCommon()
	{
		ArrayList<String[]> arrLstMatrix = new ArrayList<String[]>();
		ArrayList<String> arrLstLine = null ;
		for (Request req: this)          
		{
			arrLstLine = new ArrayList<String>();
			//add request id
			arrLstLine.add(Integer.toString(req.getRequestId()));
			// add initiator name
			if (req.getInitiator()!= null )
			{
				arrLstLine.add(req.getInitiator().toString());
			}
			else
			{
				arrLstLine.add(new String("not assigned"));
			}
			// add info system name
			if (req.getInfoSystem()!= null )
			{
				arrLstLine.add(req.getInfoSystem().toString());
		    }
			else
			{
				arrLstLine.add("not assigned");
			}
			// add status
			if (req.getCurrentStatus()!= null )
			{
				arrLstLine.add(req.getCurrentStatus().toString());
			}
			else
			{
				arrLstLine.add("not assigned");
			}
			//add date
			if (req.getDate()!= null )
			{
				arrLstLine.add(req.getDate().toString());
			}
			else
			{
				arrLstLine.add("not assigned");
			}
			// add whole line to array
			String[] lineStr = new String[arrLstLine.size()];
			lineStr = (String[]) arrLstLine.toArray(lineStr);
			arrLstMatrix.add(lineStr);
		}
		String[][] matrixStr = new String[arrLstMatrix.size()][arrLstLine.size()]; 
		matrixStr = (String[][]) arrLstMatrix.toArray(matrixStr); //cast to matrix of strings
		return  (String[][]) matrixStr;

		
	}

	
/*	
	// The follwoing method depends heavily on java reflection and might be slow and problametic. for common use it is advised to use requestArrayToMatrixCommon 
public String[][] requestArrayToMatrixGeneral(String ... args)
{
	Class reqClass = Request.class;
	ArrayList<String[]> arrLstMatrix = new ArrayList<String[]>();
	ArrayList<String> arrLstLine = null ;
	try
	{
		for (Request req: this) //go over all requests
		{
			arrLstLine = new ArrayList<String>();
			for (String field : args) //go over all fields name given as arguments
			{
					Method getter = null;
					try {
						getter = reqClass.getMethod("get"+field,Void.class ); //try and find a getter method for this field
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						System.out.println("get"+field);
						e.printStackTrace();
					}
					if (getter!= null)
					{
						try {
							arrLstLine.add(getter.invoke(req, Void.class).toString()); //call the getter in oredr to get the needed value
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						//throw no such getter exeption
					}
			
					String[] lineStr = new String[arrLstLine.size()]; 
					lineStr = (String[]) arrLstLine.toArray(lineStr); //cast to matrix of strings
					arrLstMatrix.add(lineStr);  
			}
			
		}
		
		}
	finally
	{
		//handle all exeptions
		
	}
	String[][] matrixStr = new String[arrLstMatrix.size()][arrLstLine.size()]; 
	matrixStr = (String[][]) arrLstMatrix.toArray(matrixStr); //cast to matrix of strings
	return  (String[][]) arrLstMatrix.toArray();

}
*/
		
		//	private String[] evaluationCommonFields = {"Request ID", "Initiator Name", "inspector name","info system","Notes"};
/**
 * matrix of string with data for evluation
 * @return
 */
		public String[][] requestArrayToMatrixEvaluationCommon()
		{
			
			ArrayList<String[]> arrLstMatrix = new ArrayList<String[]>();
			ArrayList<String> arrLstLine = null ;
			for (Request req: this)          
			{
	
				arrLstLine = new ArrayList<String>();
				//add request id

				arrLstLine.add(Integer.toString(req.getRequestId()));
				// add initiator name
				if (req.getInitiator()!= null )
				{
					arrLstLine.add(req.getInitiator().toString());
				}
				else
				{
					arrLstLine.add(new String("not assigned"));
				} //add inspector
				if (req.getInspector()!= null )
				{
					arrLstLine.add(req.getInspector().toString());
				}
				else
				{
					arrLstLine.add(new String("not assigned"));
				}
				// add info system name
				if (req.getInfoSystem()!= null )
				{
					arrLstLine.add(req.getInfoSystem().toString());
			    }
				else
				{
					arrLstLine.add("not assigned");
				}
				// add status
				
				//add notes
				RequestMainStatusEnum mStatus  = req.getCurrentStatus().getStatus().getmStatus();
				  RequestStepStatusEnum sStatus = req.getCurrentStatus().getStatus().getsStatus();
				  if (sStatus != null)
				  {
					if (sStatus == RequestStepStatusEnum.EVL_EVLUATION_EXTENDED || sStatus == RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_EXTENDED)
					{
						arrLstLine.add(new String("Time was extended"));
					}
					else
					{
						if (sStatus == RequestStepStatusEnum.EVL_EVLUATION_EXTENDED_DENY || sStatus == RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_DENIED)
						{
							arrLstLine.add(new String("Time extension was denied by the inspector"));
						}
						else
						{
							if (sStatus == RequestStepStatusEnum.EVL_EVLUATION_TIME_EXTENSION_PENDING || sStatus == RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_PENDING)
							{
								arrLstLine.add(new String("Time extension was requested"));
							}
						}
					}
				  }
				  else //status is null
				  {
					  arrLstLine.add("NO STATUS!!");
					  System.out.println("Warning! no status in RequestArray::requestArrayToMatrixEvaluationCommon ");
					  
				  }
				// add whole line to array
				String[] lineStr = new String[arrLstLine.size()];
				lineStr = (String[]) arrLstLine.toArray(lineStr);
				arrLstMatrix.add(lineStr);
			}
	
			if (arrLstMatrix.size() == 0 ) 
			{
				System.out.println("arrLstMatrix.size() == 0 ");
				 return null;

			}
			String[][] matrixStr = new String[arrLstMatrix.size()][arrLstLine.size()]; 
			matrixStr = (String[][]) arrLstMatrix.toArray(matrixStr); //cast to matrix of strings
			return  (String[][]) matrixStr;

			
		}

}
